<?php

include '../include/init.php';

$uid=$_POST['u']+0;
$type=trim($_POST['t']);
$comet=new cometModel();

if($type=="g60")//获取60条消息，并查看前后是否还有消息
{
	$offset=$_POST['of']+0;
	$desc=$_POST['dc']+0;

	$fid=$_POST['f']+0;
	$cmall=array();
	
	$cms=$comet->getboth($uid,$fid,$offset,$desc);
	if(!empty($cms))
	{
		($desc==0||$desc==-1)?sort($cms):$cms;
		$cmall['cms']=$cms;

		$sid=$cms[0]['id'];
		$eid=$cms[count($cms)-1]['id'];
		
		$scount=$comet->getoffsetcount($uid,$fid,$sid,0);	
		$cmall['last']=($scount&&$scount>0)?1:0;
			
		$ecount=$comet->getoffsetcount($uid,$fid,$eid,1);
		$cmall['next']=($ecount&&$ecount>0)?1:0;



		die(json_encode($cmall));
	}
	else
	{
		die(json_encode(false));
	}
	
}
else if($type=="getuper")//上拉获取3或50条历史消息，并查看前是否还有消息
{
	$offset=$_POST['of']+0;
	$need=$_POST['need']+0;

	$fid=$_POST['f']+0;
	$cmall=array();
	
	$cms=$comet->getboth($uid,$fid,$offset,0,$need);
	if(!empty($cms))
	{
		//sort($cms);
		$cmall['cms']=$cms;

		$sid=$cms[0]['id'];		
		$scount=$comet->getoffsetcount($uid,$fid,$sid,0);	
		$cmall['last']=($scount&&$scount>0)?1:0;

		die(json_encode($cmall));
	}
	else
	{
		die(json_encode(false));
	}
	
}
else if($type=='search')//搜索消息
{
	$fid=$_POST['f']+0;
	$key=trim($_POST['key']);
	if($key=='')
	{
		die(json_encode(false));
	}
	$range=$_POST['rg']+0;
	!$range?$range=0:'';
	$cms=$comet->search($uid,$fid,$key,$range);
	if(!empty($cms))
	{
		sort($cms);
		die(json_encode($cms));
	}
	else
	{
		die(json_encode('no'));
	}
}
else if($type=="gbt60")//搜索该消息的前后60条消息
{
	$fid=$_POST['f']+0;
	$cmid=$_POST['cmid']+0;
	$cmall=array();
	
	$cms=$comet->getbetween($uid,$fid,$cmid);
	if(!empty($cms))
	{
		$cmall['cms']=$cms;
		$sid=$cms[0]['id'];
		$eid=$cms[count($cms)-1]['id'];
		
		$scount=$comet->getoffsetcount($uid,$fid,$sid,0);	
		$cmall['last']=($scount&&$scount>0)?1:0;
			
		$ecount=$comet->getoffsetcount($uid,$fid,$eid,1);
		$cmall['next']=($ecount&&$ecount>0)?1:0;

		die(json_encode($cmall));
	}
	else
	{
		die(json_encode(false));
	}
	
}
else if ($type=='getdate')//获取有消息的日期
{
	$fid=$_POST['f']+0;
	$date=trim($_POST['d']);
	//$timestamp=strtotime($date);
	$current_timezone = date_default_timezone_get();
	date_default_timezone_set('Asia/Shanghai');
	$ds=$comet->getdate($uid,$fid,strtotime($date));
	date_default_timezone_set($current_timezone);
	if(!empty($ds))
	{
		die(json_encode($ds));
	}
	else
	{
		die(json_encode('no'));
	}
}
else if($type=='gbd')//从日期获取消息
{
	$fid=$_POST['f']+0;
	$date=trim($_POST['d']);

	$cmall=array();
	$current_timezone = date_default_timezone_get();
	date_default_timezone_set('Asia/Shanghai');
	$cms=$comet->getbydate($uid,$fid,strtotime($date));
	date_default_timezone_set($current_timezone);
	if(!empty($cms))
	{
		$cmall['cms']=$cms;
		$sid=$cms[0]['id'];
		$eid=$cms[count($cms)-1]['id'];
		
		$scount=$comet->getoffsetcount($uid,$fid,$sid,0);	
		$cmall['last']=($scount&&$scount>0)?1:0;
			
		$ecount=$comet->getoffsetcount($uid,$fid,$eid,1);
		$cmall['next']=($ecount&&$ecount>0)?1:0;

		die(json_encode($cmall));
	}
	else
	{
		die(json_encode(false));
	}
}